<!--main-contentcontainer
 * @Description: 
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-10-09 11:31:13
 * @LastEditors: wiz
 * @LastEditTime: 2023-10-12 11:17:14
-->
<template>
  <div class="init-container">
    <o-layout
      ref="layout"
      :listenType="listenType"
      :config="funConfig"
      :parmas="parmas"
    >
    </o-layout>
  </div>
</template>

<script lang="ts">
import { I_cmd, T_srv, Tcmd, Tsrv, funConfig, ifs, vue } from './config';

@T_srv.comp
export default class cProcessMgmt extends vue {
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  @T_srv.prop({ default: () => {}, required: true, type: Object })
  config!: any;

  @T_srv.prop({ type: Object })
  parmas: any;

  private name: string = 'cProcessMgmt';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  /* 指挥部 */
  private GT_command: I_cmd = Tcmd;

  private funConfig = funConfig();

  private listenType: string = 'default';

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.GT_command.init(this, this.GT_compBoxInfo);
    this.listenType = this.config?.listenType || 'default';
    this.init();
  }

  private init() {
    const funInfo = this.funConfig.config.second.config.first.config.funInfo;
    Tsrv.store.commit('SET_FUNINFO', {
      key: 'process_mgmt',
      val: funInfo
    });
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$bus.$emit('compBoxInfo', this.GT_compBoxInfo);
    this.receiveEvent();
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
  }

  private receiveEvent() {
    if (!Tsrv.noValue(this.config?.messageConfig?.custDefListener?.search)) {
      this.config?.messageConfig?.custDefListener?.search.forEach(EID => {
        this.$mbus.$on(EID, val => {
          if (EID.length === 8) {
            if (!Tsrv.noValue(val)) {
              // val.listenType = 'default';
              // this.$bus.$emit(
              //   this.funConfig.config.second.config.first.config.messageConfig
              //     .custDefListener.coordination,
              //   val
              // );
              // this.funConfig.config.second.config.first.config.listenType =
              //   'magicbox';
              this.listenType = 'magicbox';
              this.$mbus.$emit(
                this.funConfig.config.second.config.first.config.messageConfig
                  .custDefListener.search[1],
                val
              );
            }
          }
        });
      });
    }
  }
}
</script>
<style lang="scss" scoped></style>
